package com.hibuy.pc.mapper.shiro;

import com.hibuy.pc.model.shiro.Permission;
import com.hibuy.pc.model.shiro.Role;
import com.hibuy.pc.model.shiro.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * Created by admin on 2017/12/27.
 */
@Mapper
public interface PermissionMapper {

    @Select("select * from sys_user where email = #{email}")
    User findByUserEmail(@Param("email") String email);

    @Select("SELECT sr.name FROM sys_role sr INNER JOIN sys_user_role sur ON sr.id = sur.rid WHERE sur.uid = #{userId}")
    Set<String> getRolesName(@Param("userId") Integer userId);

    @Select("SELECT sr.* FROM sys_role sr INNER JOIN sys_user_role sur ON sr.id = sur.rid WHERE sur.uid = #{userId}")
    List<Role> getRoleList(@Param("userId") Integer userId);

    @Select("SELECT sp.`name` FROM sys_permission sp INNER JOIN sys_role_permission srp ON sp.id = srp.pid WHERE srp.rid = #{roleId}")
    Collection<String> getPermissionsName(@Param("roleId")Integer roleId);

    @Select("SELECT * FROM sys_permission")
    List<Permission> getPermissions();

    @Insert("INSERT INTO sys_permission (url,name,parent_id) VALUES (#{url},#{name},#{parentId})")
    Permission addPermission(Permission permission);
}
